package com.elephant.core.socket.socket3;

import java.io.*;
import java.net.Socket;

/**
 * 3、多个客户端连接同一个服务端
 * @author hai
 * @description
 * @date 14:05 2018/8/31
 */
public class Client {

    public static void main(String[] args) throws IOException {

        String host = "127.0.0.1";
        Integer port = 8888;
        Socket socket = new Socket(host,port);
        Writer writer = new OutputStreamWriter(socket.getOutputStream());
        writer.write("hello world");
        writer.write("eof\n");
        writer.flush();

        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        StringBuilder sb = new StringBuilder();
        String temp;
        int index;
        while ((temp = reader.readLine()) != null){
            if((index = temp.indexOf("eof")) != -1){
                sb.append(temp, 0 , index);
                break;
            }
            sb.append(temp);
        }
        System.out.println("From Server:" + sb);

        writer.close();
        reader.close();
        socket.close();
    }
}
